//
//  PayEvaluationViewController.m
//  WheatPlan
//
//  Created by 王晨辉 on 15/6/2.
//  Copyright (c) 2015年 IOSTeam. All rights reserved.
//

#import "PayEvaluationViewController.h"
#import "PayMoneyViewController.h"
@interface PayEvaluationViewController ()<UITextFieldDelegate>{
    
    
    UIImageView * downImage;
}

@end

@implementation PayEvaluationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    ////NSLog(@"%@,%@",self.time,self.type);
    
    
    //设置导航栏头文字
    self.navigationItem.title = @"同城解读";
    //设置导航栏背景颜色
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#44afd7" alpha:1]];
    //设置导航栏字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    self.view.backgroundColor = UIColorWithRGBA(240, 239, 245, 1);
    
    
    //自定义左右按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
                      forState:UIControlStateNormal];
    [button addTarget:self action:@selector(turntolast)
     forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 20, 15);
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = menuButton;
    [self subView];
    
}


-(void)turntolast{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    

}
-(void)subView{
    
    
    UIImageView * upImage = [[UIImageView alloc]init];
    upImage.frame = CGRectMake(0, NavbarHight, SCREEN_WIDTH, SCREEN_HEIGHT/4.3);
    upImage.image = [UIImage imageNamed:@"_0001_圆角矩形-2"];
    [self.view addSubview:upImage];
    
    
    downImage = [[UIImageView alloc]init];
    downImage.frame = CGRectMake(10, NavbarHight+SCREEN_HEIGHT/4.3+30+10, SCREEN_WIDTH-20, SCREEN_HEIGHT/3);
    downImage.image = [UIImage imageNamed:@"报名线框@2x.png.png"];
    downImage.userInteractionEnabled = YES;
    [self.view addSubview:downImage];
    
    
    UIButton * nowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    nowBtn.frame = CGRectMake(80, SCREEN_HEIGHT-60, SCREEN_WIDTH-160, 40);
//    nowBtn.backgroundColor = UIColorWithRGBA(255, 117, 118, 1);
    [nowBtn addTarget:self action:@selector(got:) forControlEvents:UIControlEventTouchUpInside];
    [nowBtn setTitle:@"立即报名" forState:UIControlStateNormal];
    [nowBtn setBackgroundImage:[UIImage imageNamed:@"red@2x"] forState:UIControlStateNormal];
    [nowBtn setBackgroundImage:[UIImage imageNamed:@"redScl@2x"] forState:UIControlStateHighlighted];
    [nowBtn setTintColor:[UIColor whiteColor]];
    nowBtn.layer.cornerRadius = 8;
    [self.view addSubview:nowBtn];
    
    UILabel * timeLab = [self andfrom:CGRectMake(65, (SCREEN_HEIGHT/4.3)/2-35, SCREEN_WIDTH-75, 30) andtext:self.type andfont:[UIFont systemFontOfSize:15] andAlignment:NSTextAlignmentLeft];
    [upImage addSubview:timeLab];

    UILabel * ParentLab = [self andfrom:CGRectMake(65, (SCREEN_HEIGHT/4.3)/2+5, SCREEN_WIDTH, 30) andtext:self.time andfont:[UIFont systemFontOfSize:15] andAlignment:NSTextAlignmentLeft];
    [upImage addSubview:ParentLab];

    UILabel * otherLab = [self andfrom:CGRectMake(15, upImage.frame.origin.y+upImage.frame.size.height+5, 300, 30) andtext:@"报名信息：" andfont:[UIFont systemFontOfSize:15] andAlignment:NSTextAlignmentLeft];
    [self.view addSubview:otherLab];

    UIImageView * iconImage = [[UIImageView alloc]initWithFrame:CGRectMake(35, (SCREEN_HEIGHT/4.3)/2-35+4, 22, 24)];
    iconImage.image = [UIImage imageNamed:@"定位.png"];
    [upImage addSubview:iconImage];
    
    UIImageView * timeImage = [[UIImageView alloc]initWithFrame:CGRectMake(35, (SCREEN_HEIGHT/4.3)/2+5+5, 22, 22)];
    timeImage.image = [UIImage imageNamed:@"timep.png"];
    [upImage addSubview:timeImage];
    
    babyNameTextField = [self andText: @"     输入宝宝姓名" andColo:UIColorWithRGBA(226, 227, 230, 1) andFrom:CGRectMake(20, 40, SCREEN_WIDTH-60, 30)];
    nameTextField = [self andText:@"    输入联系人" andColo:UIColorWithRGBA(226, 227, 230, 1) andFrom:CGRectMake(20, 40+30+12, SCREEN_WIDTH-60, 30)];
    numberTextField = [self andText:@"    输入联系方式" andColo:UIColorWithRGBA(226, 227, 230, 1) andFrom:CGRectMake(20, 40+(30+12)*2, SCREEN_WIDTH-60, 30)];
    
    UILabel * payLab = [self andfrom:CGRectMake(SCREEN_WIDTH-115, downImage.frame.origin.y+downImage.frame.size.height+7, 110, 30) andtext:@"需支付:           元" andfont:[UIFont systemFontOfSize:15] andAlignment:NSTextAlignmentLeft];
    [self.view addSubview:payLab];
    
    UILabel * numberLab = [self andfrom:CGRectMake(SCREEN_WIDTH-67, downImage.frame.origin.y+downImage.frame.size.height+8, 40, 30) andtext:[NSString stringWithFormat:@"%@",self.price] andfont:[UIFont systemFontOfSize:20] andAlignment:NSTextAlignmentLeft];
    numberLab.textColor = [UIColor orangeColor];
    [self.view addSubview:numberLab];
    
    
    
}
- (BOOL)isChinese:(NSString *)msg{
    
    for (int i = 0; i < [msg length]; i ++) {
        
        unichar a = [msg characterAtIndex:i];
        
        if ((int)a < 255) {
            
            return NO;
        }
    }
    
    return YES;
}

/** UILabel方法 */
-(UILabel *) andfrom:(CGRect)frome andtext:(NSString *)text andfont:(UIFont *)font andAlignment:(NSTextAlignment)Alignment{
    
    UILabel * lab_tp = [[UILabel alloc]initWithFrame:frome];
    lab_tp.adjustsFontSizeToFitWidth = YES;
    lab_tp.text = text;
    lab_tp.textAlignment = Alignment;
    lab_tp.font = font;
    lab_tp.numberOfLines = 1;
    return lab_tp;
    
}


/** UITextField方法 */
-(UITextField *) andText:(NSString *)text andColo:(UIColor *)colo andFrom:(CGRect)frome {
    
    UITextField * TextField = [[UITextField alloc]init];
    TextField.frame = frome;
    TextField.borderStyle = 1;
    TextField.placeholder = text;
    TextField.layer.borderColor=colo.CGColor;
    TextField.layer.borderWidth= 1.0f;
    TextField.delegate = self;
    [downImage addSubview:TextField];
    return TextField;
    
}
- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    if (mobileNum.length != 11)
    {
        return NO;
    }
    /**
     * 手机号码:
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9]
     * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178
     * 联通号段: 130,131,132,155,156,185,186,145,176
     * 电信号段: 133,153,180,181,189,177
     */
    
    
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9])\\d{8}$";
    /**
     * 中国移动：China Mobile
     * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178
     */
    NSString *CM = @"^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$";
    /**
     * 中国联通：China Unicom
     * 130,131,132,155,156,185,186,145,176
     */
    NSString *CU = @"^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$";
    /**
     * 中国电信：China Telecom
     * 133,153,180,181,189,177
     */
    NSString *CT = @"^1(33|53|77|8[019])\\d{8}$";
    
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}


-(void)got:(UIButton *)sent{
    
    
    
//    nameTextField = [
//                     numberTextField =
    
    if (babyNameTextField.text.length!=0&&nameTextField.text.length!=0&&numberTextField.text.length!=0) {
        
        
        
        
        if(![self isMobileNumber:numberTextField.text]) {
            [JKAlert showMessage:@"提示" message:@"请正确输入手机号码。"];
        }else  if (![self isChinese:nameTextField.text] ) {
            ////NSLog(@"请选择地址");
            [JKAlert showMessage:@"提示" message:@"宝宝姓名只能为中文"];
            
        } else{
            
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString *ID = [user objectForKey:@"ID"];
            
            
            NSDictionary * dic = @{@"userId":ID,@"activityId":self.activityId,@"contact":nameTextField.text,@"cellNumber":numberTextField.text,@"typeId":@"2",@"babyName":babyNameTextField.text,};
            [UserDAL getActivityJoineWithParams:dic withBlock:^(NSDictionary *data, NSError *error) {
                
                
                
                if (data) {
                    
                    NSString * str = [data objectForKey:@"msg"];
                    ////NSLog(@"111111%@",str);
                    
                    [JKAlert showMessage:@"提示" message:str];
                    
                    
                    
                } else {
                    NSString * str = [data objectForKey:@"msg"];
                    [JKAlert showMessage:@"提示" message:str];
                    ////NSLog(@"222222");
                    
                }
                
                
                
            }];
            
            
            
            PayMoneyViewController * payMoneyVC = [[PayMoneyViewController alloc]init];
            payMoneyVC.priceStr = self.price;
            payMoneyVC.name = self.name;
            UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:payMoneyVC];
            [self presentViewController:nav animated:YES completion:nil];
            

            
        }
        
        
    }else{
        
        
        [JKAlert showMessage:@"提示" message:@"请完善您的信息"];
        
    }
    
    
    

    
    
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [babyNameTextField resignFirstResponder];
    [nameTextField resignFirstResponder];
    [numberTextField resignFirstResponder];

    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
